package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.query.EmployeeQueryObject;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Description TODO
 * @ClassName EmployeeController
 * @Author luhui
 * @date 2020.06.13 12:58
 * @version: 1.0
 */

@Controller
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/employee/list")
    @RequiredPermission(values = {"员工列表","employee:list"})
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject queryObject){
        model.addAttribute("result",employeeService.query(queryObject));
        model.addAttribute("depts",departmentService.listAll());
        return "employee/list";
    }

    @RequestMapping("/employee/delete")
    @RequiredPermission(values = {"员工删除","employee:delete"})
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/employee/input")
    @RequiredPermission(values = {"员工编辑","employee:saveOrUpdate"})
    public String input(Model model,Long id){
        if (id != null){
            model.addAttribute("e",employeeService.get(id));
        }
        model.addAttribute("depts",departmentService.listAll());
        model.addAttribute("roles",roleService.listAll());
        return "employee/input";
    }

    @RequestMapping("/employee/saveOrUpdate")
    @RequiredPermission(values = {"员工新增或删除","employee:saveOrUpdate"})
    public String saveOrUpdate(Employee employee,Long[] ids){
        employeeService.saveOrUpdate(employee,ids);
        return "redirect:/employee/list.do";
    }
}